<?php

/**
 * AjaxErrorController
 * Handler errors for AjaxController
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class AjaxErrorController extends Zend_Controller_Action {
	
	public function createErrorResult($msg) {
		return array ('result' => 'internal-error', 'message' => $msg );
	}
	
	public function errorAction() {
		$errors = $this->_getParam ( 'error_handler' );
		if ($errors == null || ! $errors instanceof ArrayObject) {
			//maybe the user is trying to access this page directly?
			return;
		}
		switch ($errors->type) {
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE :
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER :
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION :
				
				$this->view->response = $this->createErrorResult ( 'Page not found' );
				break;
			case ExceptionConfig::PERM_EXCEPTION :
				$this->getResponse ()
					->setHttpResponseCode ( 403 );
				$this->view->response = $this->createErrorResult ( 'Access denied' );
				break;
			default :
				// application error
//				$this->getResponse ()
//					->setHttpResponseCode ( 500 );
//				$this->view->response = $this->createErrorResult ( 'Application error: '.$errors->exception->getMessage() );
				$this->view->response = $this->createErrorResult ( 'Application error' );
				break;
		}
		
		echo $this->_helper
			->renderAjaxResponse ();
	}
}
